﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common.ValueObject;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Tool.UserControls;

namespace ERP_RORZE_TOOL.Tool.Forms
{
    public partial class frm_SupplierAdd : DevExpress.XtraEditors.XtraForm
    {
        private ucSupplier parent;
        public frm_SupplierAdd(ucSupplier fSupplier)
        {
            InitializeComponent();
            btAdd.Image = ERP_RORZE_TOOL.Properties.Resources.add;
            btCancel.Image = ERP_RORZE_TOOL.Properties.Resources.cancel;
            parent = fSupplier;
            this.CancelButton = this.btCancel;

        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            SupplierHelper helper = new SupplierHelper(new Supplier());
            if (GetData(helper.ValueObject))
            {


                if (helper.Insert() > 0)
                {

                    if (Utilities.MessageConfirmationYesNo(ToolConstants.Message.AddSuccessAndQuestion))
                    {

                        txtSupplierID.Text = "";
                        txtSupplierName.Text = "";
                        txtSupplierID.Focus();
                    }
                    else
                    {
                        this.Close();
                    }
                }
                else
                {

                    Utilities.MessageErrorOk(ToolConstants.Message.AddFailed);
                }
                parent._action = 0;
                parent.BindDatatoGrid();
            }
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }
        #region Private methods

        private bool CheckDuplicate(string supplierId)
        {
            SupplierHelper helper = new SupplierHelper(new Supplier());
            helper.ValueObject.SupplierID = supplierId;
            DataTable dt = helper.GetOne();
            if (dt.Rows != null && dt.Rows.Count > 0)
                return true;
            return false;

        }
        private bool GetData(Supplier o)
        {
            #region Check valid
            if (Utilities.IsNullOrEmpty(txtSupplierID.Text.Trim()))
            {
                MessageBox.Show(ToolConstants.Message.Supplier.SupplierIdCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtSupplierName.Text.Trim()))
            {
                MessageBox.Show(ToolConstants.Message.Supplier.SupplierNameCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (CheckDuplicate(txtSupplierID.Text.Trim()))
            {
                MessageBox.Show(ToolConstants.Message.Supplier.SupplierHasExisted, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            #endregion

            o.SupplierID = txtSupplierID.Text;
            o.SupplierName = txtSupplierName.Text;

            return true;
        }

        #endregion

    }
}